<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>文本框组背景色变化</title>
  </head>
  <body>
    <div id="box">
      <input type="text" /><br />
      <input type="text" /><br />
      <input type="text" /><br />
      <input type="text" /><br />
      <input type="text" /><br />
      <input type="text" /><br />
    </div>
    <script>
      //获取元素
      var box = document.getElementById("box");
      var inputs = box.children;
      //为每个input添加聚焦focus事件
      for (var input of inputs) {
        input.onfocus = focus;
      }
      //聚焦函数，当前正在输入的input背景置灰，同组其他input背景置为白色
      function focus(e) {
        for (var input of inputs) {
          input.style.backgroundColor = "white";
        }
        e.target.style.backgroundColor = "grey";
      }
    </script>
  </body>
</html>
